-- ==== BEGIN LICENSE BLOCK ====
-- storyboard-addon - Copyright (C) Arnaud Charnoz, 2006-2010.
-- Distributed under the terms of the GNU Lesser General Public License (LGPL) as
-- published by the Free Software Foundation.
-- ===== END LICENSE BLOCK ====

-------------------------------------------------------------------------------
-- [ LES PETITES FONCTIONS UTILES ] --
-------------------------------------------------------------------------------

-- [ Function qui renvoie true si le joueur est dans le meme etat que le parametre ] --
function storyboard_isTheSameLifeStateOnPlayer(playerLife)
   local vie =  UnitHealth("player");
   if vie==1 then
      return playerLife == 0
   else
      return playerLife ~= 0
   end
end


-- [ Function qui renvoie true si la cible est dans le meme etat que le parametre ] --
function storyboard_isTheSameLifeStateOnTarget(targetLife)
   local vie = UnitHealth("target");
   if vie==0 then
      return targetLife == 0
   else
      return targetLife ~= 0
   end
end


-- [ Function qui renvoie true si les objets n�c�ssaires sont en votre possession ] --
function storyboard_haveYouGotObjects(neededObjects)
   local haveAllObjects = true
   for idNewObjet in neededObjects do
      local trigObj = neededObjects[idNewObjet]
      haveAllObjects = haveAllObjects and STORYBOARD_SAVED_OBJECT[trigObj]
   end
   return haveAllObjects
end


-- [ Function qui renvoie true si un des mots cl�s est contenu dans votre r�ponse ] --
function storyboard_areMatchBetweenYourAnswerAndYourWords(answer,keywords)
   local oneOfKeywordsMatch = false
   for idWords in keywords do
      local match = string.find(string.lower(answer),string.lower(keywords[idWords])) ~= nil
      oneOfKeywordsMatch = oneOfKeywordsMatch or match
   end
   return oneOfKeywordsMatch
end


-- [ Function qui renvoie true si votre cible correpond a un des noms ] --
function storyboard_isTheGoodTarget(names)
   local oneOfKeywordsMatch = false
   local npc = UnitName("target")
   for idWords in names do
      local match = string.find(string.lower(npc),string.lower(names[idWords])) ~= nil
      oneOfKeywordsMatch = oneOfKeywordsMatch or match
   end
   return oneOfKeywordsMatch
end


-- [ Function qui cache les objets qui seront perdues apr�s votre interaction ] --
function storyboard_setLostObjects(lostObjects)
   local mes = " ";
   local haveLostObjects = false
   for idNewObjet in lostObjects do
      local trigObj = lostObjects[idNewObjet]

      local name = " ";
      for idObject = 1, STORYBOARD_FS_NB_OBJECT, 1 do
     local id = STORYBOARD_SCENARIO_OBJECT[idObject].id;
     if id == trigObj then
      name = STORYBOARD_SCENARIO_OBJECT[idObject].name
     end
      end
  
      STORYBOARD_SAVED_OBJECT[trigObj] = false;

      haveLostObjects = true;
   end
   
   if  haveLostObjects then
   mes = " ";
      storyboard_sendText(mes);
      mes = STORYBOARD_MES_INTERFACE_010;
      storyboard_sendTextRed(mes);
      storyboard_function_resetBag()
   end
   
end


-- [ Function qui vous mets les nouveaux objets dans votre sac ] --
function storyboard_setNewObjects(newObjects)
   local mes = " ";
   local haveNewObjects = false
   for idNewObjet in newObjects do
      local trigObj = newObjects[idNewObjet]
      
      local name = " ";
      for idObject = 1, STORYBOARD_FS_NB_OBJECT, 1 do
     local id = STORYBOARD_SCENARIO_OBJECT[idObject].id;
     if id == trigObj then
      name = STORYBOARD_SCENARIO_OBJECT[idObject].name
     end
      end
      
      STORYBOARD_SAVED_OBJECT[trigObj] = true;
      haveNewObjects = true
      
   end
   
   if  haveNewObjects then
      mes = " ";
      storyboard_sendText(mes);
      mes = STORYBOARD_MES_INTERFACE_011;
      storyboard_sendTextRed(mes);
      storyboard_function_resetBag()
   end
   
end


-- [ Function qui renvoie true si vous avez besoin de mots cl�s pour l'event] --
function storyboard_AreYouNeedKeywords(keywords)
 return keywords[1] ~= nil
end


-- [ Function qui renvoie true si vous avez besoin d'objets pour l'event] --
function storyboard_AreYouNeedObjects(neededObjects)
 return neededObjects[1] ~= nil
end




--===========================================================================--
-- [ MANAGEMENT OF THE BAG ] --
--===========================================================================--

--[ permet d'avancer dans le sac ]--
function storyboard_function_previousPartOfBag()
 if( STORYBOARD_NAME_ITEM_CURRENT > 5 ) then
 STORYBOARD_NAME_ITEM_CURRENT = STORYBOARD_NAME_ITEM_CURRENT - 5
 storyboard_function_recomputeItemVisu()
 end
end

--[ permet de reculer dans le sac ]--
function storyboard_function_nextPartOfBag()
storyboard_debugMessage(STORYBOARD_NAME_ITEM_CURRENT);
   if storyboard_function_howManyItemsAreVisible() >= ( STORYBOARD_NAME_ITEM_CURRENT + 5 ) then
   STORYBOARD_NAME_ITEM_CURRENT = STORYBOARD_NAME_ITEM_CURRENT + 5
   storyboard_function_recomputeItemVisu()
   end
    storyboard_debugMessage(STORYBOARD_NAME_ITEM_CURRENT); 
end

--[ permet d'avancer dans le sac ]--
function storyboard_function_resetBag()
   STORYBOARD_NAME_ITEM_CURRENT = 1
   storyboard_function_recomputeItemVisu()
end

--[ permet de savoir combien d'objet vous possedez ]--
function storyboard_function_howManyItemsAreVisible()
   local cmpt = 0;
   for idObject = 1, STORYBOARD_FS_NB_OBJECT, 1 do
     local id = STORYBOARD_SCENARIO_OBJECT[idObject].id;
     if STORYBOARD_SAVED_OBJECT[id] then
    cmpt = cmpt + 1
     end
   end
   return cmpt
end

--[ recreer le bon affichage ]--
function storyboard_function_recomputeItemVisu()
   
   SbQuestBarFrameButton13Icon:SetTexture(STORYBOARD_NAME_ITEM_EMPTY);
   SbQuestBarFrameButton14Icon:SetTexture(STORYBOARD_NAME_ITEM_EMPTY);
   SbQuestBarFrameButton15Icon:SetTexture(STORYBOARD_NAME_ITEM_EMPTY);
   SbQuestBarFrameButton16Icon:SetTexture(STORYBOARD_NAME_ITEM_EMPTY);
   SbQuestBarFrameButton17Icon:SetTexture(STORYBOARD_NAME_ITEM_EMPTY);
   
   STORYBOARD_NAME_ITEM_1 = STORYBOARD_MES_EMPTYPLACEMENT;
   STORYBOARD_NAME_ITEM_2 = STORYBOARD_MES_EMPTYPLACEMENT;
   STORYBOARD_NAME_ITEM_3 = STORYBOARD_MES_EMPTYPLACEMENT;
   STORYBOARD_NAME_ITEM_4 = STORYBOARD_MES_EMPTYPLACEMENT;
   STORYBOARD_NAME_ITEM_5 = STORYBOARD_MES_EMPTYPLACEMENT;
   STORYBOARD_NAME_ITEM_1_ID = 0
   STORYBOARD_NAME_ITEM_2_ID = 0
   STORYBOARD_NAME_ITEM_3_ID = 0
   STORYBOARD_NAME_ITEM_4_ID = 0
   
   local limits = storyboard_function_howManyItemsAreVisible()
   local cmpt = 0;
   for idObject = 1, STORYBOARD_FS_NB_OBJECT, 1 do
     local id = STORYBOARD_SCENARIO_OBJECT[idObject].id;
     if STORYBOARD_SAVED_OBJECT[id] then
    cmpt = cmpt + 1
    
    if cmpt >= STORYBOARD_NAME_ITEM_CURRENT and 
       cmpt < ( STORYBOARD_NAME_ITEM_CURRENT + 5 ) then

       if ( cmpt - STORYBOARD_NAME_ITEM_CURRENT ) == 0  then
       STORYBOARD_NAME_ITEM_1 = STORYBOARD_SCENARIO_OBJECT[idObject].name
       STORYBOARD_NAME_ITEM_1_ID = id
       SbQuestBarFrameButton13Icon:SetTexture(STORYBOARD_SCENARIO_OBJECT[idObject].icone)            
        
       elseif ( cmpt - STORYBOARD_NAME_ITEM_CURRENT ) == 1  then
       STORYBOARD_NAME_ITEM_2 = STORYBOARD_SCENARIO_OBJECT[idObject].name
       STORYBOARD_NAME_ITEM_2_ID = id
       SbQuestBarFrameButton14Icon:SetTexture(STORYBOARD_SCENARIO_OBJECT[idObject].icone)
       
       elseif ( cmpt - STORYBOARD_NAME_ITEM_CURRENT ) == 2  then
       STORYBOARD_NAME_ITEM_3 = STORYBOARD_SCENARIO_OBJECT[idObject].name
       STORYBOARD_NAME_ITEM_3_ID = id
       SbQuestBarFrameButton15Icon:SetTexture(STORYBOARD_SCENARIO_OBJECT[idObject].icone)
       
       elseif ( cmpt - STORYBOARD_NAME_ITEM_CURRENT ) == 3  then
       STORYBOARD_NAME_ITEM_4 = STORYBOARD_SCENARIO_OBJECT[idObject].name
       STORYBOARD_NAME_ITEM_4_ID = id
       SbQuestBarFrameButton16Icon:SetTexture(STORYBOARD_SCENARIO_OBJECT[idObject].icone)
       
       elseif ( cmpt - STORYBOARD_NAME_ITEM_CURRENT ) == 4  then
       STORYBOARD_NAME_ITEM_5 = STORYBOARD_SCENARIO_OBJECT[idObject].name
       STORYBOARD_NAME_ITEM_5_ID = id
       SbQuestBarFrameButton17Icon:SetTexture(STORYBOARD_SCENARIO_OBJECT[idObject].icone)
       end
       
    end
     end
   end
end

--[ permet d'afficher un item du sac ]--
function storyboard_function_showItemOfBag(idCurrentItem)

   for idObject = 1, STORYBOARD_FS_NB_OBJECT, 1 do
   local id = STORYBOARD_SCENARIO_OBJECT[idObject].id;
   if STORYBOARD_SAVED_OBJECT[id] and id == idCurrentItem then
   
      SBMessageManagerFrame:SetText(" ");
      local mesEmote = " ";
      local mes = " ";
      storyboard_sendTextGreen(mes)
      mes = STORYBOARD_MES_INTERFACE_006;
      mesEmote = mesEmote .. STORYBOARD_MES_INTERFACE_006;
      storyboard_sendTextGreen(mes)
      mes = STORYBOARD_SCENARIO_OBJECT[idObject].name
      mesEmote = mesEmote .. STORYBOARD_SCENARIO_OBJECT[idObject].name;      
      storyboard_sendTextYellow(mes)
      mes = STORYBOARD_MES_INTERFACE_007
      mesEmote =  mesEmote .. " - " .. STORYBOARD_MES_INTERFACE_007;
      storyboard_sendTextGreen(mes)
      mes = STORYBOARD_SCENARIO_OBJECT[idObject].info
      mesEmote = mesEmote .. STORYBOARD_SCENARIO_OBJECT[idObject].info;
      storyboard_sendTextYellow(mes)
      storyboard_writeMessageObjectForOtherPlayer(mesEmote);
   
      SbQuestFrameNpcNameText:SetText(STORYBOARD_MES_INTERFACE_008)
      storyboard_showQuestFrame()

   end
   end
end









